package cn.yzx.pethome.system.user.controller;

import cn.yzx.pethome.system.user.domain.Employee;
import cn.yzx.pethome.system.user.query.EmployeeQuery;
import cn.yzx.pethome.system.user.service.IEmployeeService;
import cn.yzx.pethome.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
//这里是说明你这个类是干什么用的
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController{
    @Autowired
    private IEmployeeService employeeService;

    @GetMapping("/list")
    @ApiOperation(value = "获取所有的员工信息" )
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    @PostMapping
    @ApiOperation(value = "分页查询")
    public AjaxResult findQuery(@RequestBody EmployeeQuery query){
        try {
            //查询展示的数据
            List<Employee> byQuery = employeeService.findByQuery(query);
            //查询总条数
            Long byQueryCount = employeeService.findByQueryCount(query);
             return AjaxResult.success().setData(byQuery).setTotal(byQueryCount);
        } catch (Exception e) {
            e.printStackTrace();
        }
            return AjaxResult.error().setMsg("查询失败");
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "员工删除")
    public AjaxResult delete(@PathVariable Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
    @PatchMapping
    @ApiOperation(value = "员工批量删除")
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            employeeService.batchDel(ids);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
            return AjaxResult.error().setMsg("删除失败");
    }
    @PutMapping
    @ApiOperation(value = "员工编辑")
    public AjaxResult save(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){
                employeeService.add(employee);
            }else{
                employeeService.update(employee);
            }
            return AjaxResult.success().setMsg("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("操作失败");
    }
}

